import request from "@/service/request";
import { PromisePageResponse, PromiseResponse } from "@/service/api/type.ts";
import {
  OrganizationNode,
  RegionNode, SysDictDataVo,
  SysOrganizationVo,
} from "@/service/api/system/organization/types.ts";

/**
 * 获取组织树
 * @param query
 */
const getOrganizationTree = (
  query: any,
): PromisePageResponse<OrganizationNode> => {
  return request({
    url: "/system/organization/treeselect",
    method: "get",
    params: query,
  });
};

/**
 * 新增根组织
 * @param query
 */
const createRootOrganization = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/organization/root",
    method: "post",
    data: query,
  });
};

/**
 * 新增组织
 * @param query
 */
const createOrganization = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/organization",
    method: "post",
    data: query,
  });
};

/**
 * 获取地区树
 */
const getRegionTree = (): PromiseResponse<RegionNode[]> => {
  return request({
    url: "/region/treeselect",
    method: "get",
  });
};

/**
 * 修改组织
 * @param query
 */
const updateOrganization = (query: any): PromiseResponse<any> => {
  return request({
    url: "/system/organization/edit",
    method: "put",
    data: query,
  });
};

/**
 * 根据id获取组织详情
 * @param id
 */
const getOrganizationDetail = (
  id: string,
): PromiseResponse<SysOrganizationVo> => {
  return request({
    url: `/system/organization/${id}`,
    method: "get",
  });
};

/**
 * 删除组织
 * @param ids
 */
const deleteOrganization = (
  ids: string | Array<string>,
): PromiseResponse<any> => {
  return request({
    url: `/system/organization/${ids}`,
    method: "delete",
  });
};

/**
 * 获取所有组织类型
 */
const getAllOrganizationTypeList = (): PromisePageResponse<SysDictDataVo> => {
  return request({
    url: "/system/dict/data/list",
    method: "get",
    params: {
      dictType: "organization_type",
    },
  });
};

/**
 * 获取根组织类型
 */
const getRootOrganizationTypeList = (): PromisePageResponse<SysDictDataVo> => {
  return request({
    url: "/system/dict/data/list",
    method: "get",
    params: {
      dictType: "organization_type",
      parentId: 0
    },
  });
};

/**
 * 获取子组织类型
 */
const getSubOrganizationTypeList = (parentValue: string): PromisePageResponse<SysDictDataVo> => {
  return request({
    url: "/system/dict/data/list",
    method: "get",
    params: {
      dictType: "organization_type",
      parentValue,
    },
  });
};

export {
  getOrganizationTree,
  createRootOrganization,
  createOrganization,
  getRegionTree,
  updateOrganization,
  getOrganizationDetail,
  deleteOrganization,
  getAllOrganizationTypeList,
  getRootOrganizationTypeList,
  getSubOrganizationTypeList,
};
